var baseURL = "../../";
var vm = new Vue({
    el: '#cwapp',
    data: {
        q: {
            username: null,
            page: "1",
            limit: "10"
        },
        company:{

        },
        companyId:null,
        companyList:[],
        showCompanyList:false,
        showList: true,
        title: null,
        roleList: {},
        user: {
            status: 1,
            roleIdList: []
        },
        page: {},
        multipleSelection: [],
        rules: {
            username: [
                { required: true, message: '请输入用户名', trigger: 'blur' }
            ],
            password: [
                { required: true, message: '请输入密码', trigger: 'blur' },
                { min: 6, message: '密码最少六位', trigger: 'blur' }
            ],
            roleId: [
                {required: true, message: '请选择角色', trigger: 'change' }
            ]
        }
    },
    methods: {
        // 为HR选择公司
        selectCompany(user) {
            vm.company.userId=user.userId;
            vm.companyId=user.companyId;
            vm.showCompanyList = true;
        },
        // 监听pagesize改变事件
        handleSizeChange(val) {
            console.log(val);
            vm.q.limit = val.toString();
            this.loadPage(false);
        },
        // 监听页码值改变事件
        handleCurrentChange(val) {
            console.log(val);
            vm.q.page = val.toString();
            this.loadPage(false);
        },
        // 监听选中的表格记录
        handleSelectionChange(val) {
            this.multipleSelection = val;
            console.log(this.multipleSelection)
        },
        // 查询功能
        query: function () {
            this.loadPage(false)
        },
        // 增加功能
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.roleList = {};
            vm.user = {status: 1, roleIdList: []};
            //获取角色信息
            this.getRoleList();
        },
        // 修改功能
        update: function (event) {
            var ids=this.getSelectedIds();
            let userId;
            if (ids.length===1){
                userId=ids[0];
            }else {
                this.$message.error('请选择一条记录');
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.getInfo(userId)
            //获取角色信息
            this.getRoleList();
        },
        // 在插入数据库时，如果有某某一个主要字段的值重复，则不插入，否则则插入
        saveOrUpdate: function (event) {

            this.$refs['ruleForm'].validate((valid) => {
                if (valid) {
                    var url = vm.user.userId == null ? "sys/user/save" : "sys/user/update";
                    $.ajax({
                        type: "POST",
                        url: baseURL + url,
                        contentType: "application/json",
                        data: JSON.stringify(vm.user),
                        // data: vm.user,
                        success: function (r) {
                            if (r.code === 0) {
                                Vue.prototype.$message({
                                    type: 'success',
                                    message: '操作成功!'
                                });
                                vm.loadPage(false);
                            } else {
                                Vue.prototype.$message({
                                    type: 'error',
                                    message: r.msg
                                });
                            }
                        }
                    });
                } else {
                    return false;
                }
            });

        },
        // 删除功能
        del: function (event) {
            var ids=this.getSelectedIds();
            let userIds;
            if (ids.length===0){
                this.$message.error('请选择一条记录');
                return;
            }else {
                userIds=ids;
            }

            this.$confirm('确定要删除选中的记录?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/user/delete",
                    contentType: "application/json",
                    data: JSON.stringify(userIds),
                    success: function (r) {
                        if (r.code === 0) {
                            Vue.prototype.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                            vm.loadPage(false);
                        } else {
                            Vue.prototype.$message({
                                type: 'error',
                                message: r.msg
                            });
                        }
                    }
                });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        // 获取用户信息
        getInfo: function (userId) {
            $.get(baseURL + "sys/user/info/" + userId, function (r) {
                vm.user = r.user;
            });
        },
        // 获取角色信息列表
        getRoleList: function () {
            $.get(baseURL + "sys/role/select", function (r) {
                vm.roleList = r.list;
            });
        },
        // 载入页面
        loadPage(home) {
            let q = {}
            if (home) {
                q.page = '1'
                this.list(q);
            } else {
                vm.showList=true;
                this.list(vm.q)
            }

        },
        // 获取列表
        list(data) {
            $.ajax({
                type: "POST",
                url: baseURL + 'sys/user/list',
                contentType: "application/json",
                data: JSON.stringify(data),
                success: function (r) {
                    if (r.code === 0) {
                        vm.page = r.page
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        // 公司列表
        listCompany(){
            $.ajax({
                type: "POST",
                url: baseURL + 'dev/company/companyList',
                contentType: "application/json",
                data: null,
                success: function (r) {
                    if (r.code === 0) {
                        vm.companyList = r.list
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        // 为HR设置公司
        setCompany(){
            $.ajax({
                type: "POST",
                url: baseURL + 'dev/company/setCompany',
                contentType: "application/json",
                data: JSON.stringify(vm.company),
                success: function (r) {
                    if (r.code === 0) {
                        Vue.prototype.$message({
                            type: 'success',
                            message: '设置成功!'
                        });
                        vm.loadPage(false);
                        vm.showCompanyList = false;
                    } else {
                        Vue.prototype.$message({
                            type: 'error',
                            message: r.msg
                        });
                    }
                }
            });
        },
        // 选择公司
        chooseCompany(event){
            vm.company.companyId=vm.companyId;
        },
        // 获取选中的角色ID
        getSelectedIds: function () {
            var ids=[];
            for (var item of vm.multipleSelection) {
                ids.push(item.userId)
            }
            return ids;
        },
    },
    created: function () {
        this.loadPage(true);
        this.listCompany();
    }
});
